<div class="row">
	<div class="col-xs-12">
		<div class="box">
			<div class="box-header">
				<h3 class="box-title">资源管理</h3>
				<div class="box-tools pull-right">
						<@shiro.hasPermission name="resource:add">
						<a  class="btn btn-sm btn-primary" id="addUrl" target="modal" modal="lg" href="javascript:;">添加</a>
						</@shiro.hasPermission>
				</div>
			</div>
			<div class="box-body">
                <div class="col-md-3">
                    <div class="form-group">
                        <ul id="ztree" class="ztree"></ul>
                    </div>
                </div>
                <div class="col-md-9">
				<table id="resource_tab" class="table table-bordered table-striped">
					<thead>
						<tr>
							<tr>
								<th>名称</th>
								<th>类型</th>
								<th>URL</th>
								<th>权限</th>
								<th>状态</th>
								<th>操作</th>
							</tr>
						</tr>
					</thead>
				</table>
				</div>
			</div>
		</div>
	</div>
</div>

<link rel="stylesheet" href="plugins/ztree/metroStyle/metroStyle.css">
<script src="plugins/ztree/jquery.ztree.core.js"></script>

<script type="text/javascript">
var resource_tab;
var nowTreeId ;

$(function() {

    /*****ztree*****/
    var setting = {
        data: {
            simpleData: {
                enable: true
            }
        },

        callback: {
            onExpand:this.onExpand,
            onMouseDown: onMouseDown
        }
    };

    var zNodes =[
	<#list list as o>
		<#if o.id == 1 || o.type == 'directory' || o.type == 'menu'>
            { "id":${o.id},<#if o.type == 'directory'>"open":true,</#if> "pId":${o.parentId}, "name":"${o.name}"},
		</#if>
	</#list>
    ];

    function onExpand(event, treeId, treeNode) {
    }

    function onMouseDown(event, treeId, treeNode) {
        if(treeNode != null){
            nowTreeId = treeNode.id;
            var param = {"id" : nowTreeId};
            resource_tab.settings()[0].ajax.data = param;
            resource_tab.ajax.reload();

            $("#addUrl").attr("href","/admin/resource/add?id="+nowTreeId);
        }
    }

    $("#addUrl").on('click',function () {
        var href = $("#addUrl").attr("href");
        if (href == 'javascript:;'){
            alertMsg("请选择一级目录","warning");
            return false;
        }
    });

	//初始化树
    $.fn.zTree.init($("#ztree"), setting, zNodes);

	//初始化表格
	var No=0;
    resource_tab = $('#resource_tab').DataTable({
		"dom":'itflp',
		"processing":true,
		"searching":false,
		"paging":false,
		"bInfo":false,
        "bSort":false,
		"language":{"url":"plugins/datatables/language.json"},
		"ajax":{"url":"/admin/resource/treeDetailList","type":"post"},
		"columns":[ 
			{"data":"name"},
			{"data":"type"},
			{"data": "url"},
			{"data":"permission"},
            {"data":"status"},
			{"data":null} 
			],
		"columnDefs":[
            {
                targets: 1,
                data: null,
                render: function (data) {
                    if(data == "directory"){
                        return "目录";
                    }
                    if(data == "menu"){
                        return "菜单";
                    }
                    if(data == "button"){
                        return "按钮";
                    }
                    return "其他";
                }
            },
			{
			    targets: -2,
			    data: null,
			    render: function (data) {
			    	if(data == "0"){
			    		return "停用";
			    	}
			    	if(data == "1"){
			    		return "启用";
			    	}
			    	return "未知状态";
			    }
			},
            {
				"targets" : -1,
				"data" : null,
				"render" : function(data) {
					var btn = '<@shiro.hasPermission name="resource:read">'
							+'<a class="btn btn-xs btn-primary" target="modal" modal="lg" href="/admin/resource/view?id='+ data.id+'">查看</a>&nbsp;'
							+'</@shiro.hasPermission>'
							+'<@shiro.hasPermission name="resource:edit">'
							+'<a class="btn btn-xs btn-info"  target="modal"  modal="lg" href="/admin/resource/edit?id='+ data.id+'">修改</a>&nbsp;'
							+'</@shiro.hasPermission>'
							+'<@shiro.hasPermission name="resource:delete">'
							+'<a class="btn btn-xs btn-default " callback="resourceReload();" data-body="确认要删除吗？" target="ajaxTodo" href="/admin/resource/delete?id='+ data.id +'">删除</a>';
						    +'</@shiro.hasPermission>'
						return btn;
				}
		} ]
	}).on('xhr.dt', function ( e, settings, data ,xhr) {
        dataTableMsg(data,xhr);
    } );
});

function resourceReload(){
    var param = {"id" : nowTreeId};
    resource_tab.settings()[0].ajax.data = param;
    resource_tab.ajax.reload();
}

</script>